$(function () {
    var plandevelopment_datagrid = $("#plandevelopment_datagrid");
    var plandevelopment_dialog = $("#plandevelopment_dialog");
    var plandevelopment_form = $("#plandevelopment_form");
    var cmdObj = {
        //编辑
        edit: function () {
            //判断是否选中数据
            var row = plandevelopment_datagrid.datagrid("getSelected");

            if (!row) {
                $.messager.alert('温馨提示', "请选择要开发的客户数据");
                return;
            }
            if (row) {
                row["client.id"] = row.client.id;
                row["client.name"] = row.client.name;
                row["implement.id"] = row.implement.id;
            }

            //添加与编辑时将数据清空
            plandevelopment_form.form("clear");
            //数据回显: 同名原则
            plandevelopment_form.form("load", row);
            //1.弹框
            plandevelopment_dialog.dialog("open");
            //设置标题
            plandevelopment_dialog.dialog("setTitle", "客户信息编辑");
        },
        //保存操作
        save: function () {
            var id = $("#plandevelopmenId").val();

            url = "/plandevelopment/update";

            plandevelopment_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        //关闭框
                        plandevelopment_dialog.dialog("close");
                        //刷新列表数据
                        plandevelopment_datagrid.datagrid("reload");
                    }
                }
            })
        },
        //关闭
        cancel: function () {
            plandevelopment_dialog.dialog("close");
        }, //工具栏的刷新
        reload: function () {
            plandevelopment_datagrid.datagrid("reload");
        },
        //建议移入资源池
        changePool: function () {
            //判断是否选中数据
            var row = plandevelopment_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            row.client.state = 2;
            $.messager.confirm('确认', '您确认要开发该客户吗？', function (r) {
                if (r) {
                    $.get("/client/changeState", {id: row.client.id,state:row.client.state}, function (data) {
                        if (data.success) {
                            //刷新列表数据
                            plandevelopment_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },
        //开发成功
        dpsuccess: function () {
            //判断是否选中数据
            var row = plandevelopment_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            row.client.state = 5;
            $.messager.confirm('确认', '您确认要开发该客户吗？', function (r) {
                if (r) {
                    $.get("/client/changeState", {id: row.client.id,state:row.client.state}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', "恭喜你又拿下一单");
                            //刷新列表数据
                            plandevelopment_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },
    }
    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    plandevelopment_datagrid.datagrid({
        url: "plandevelopment/list",
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        toolbar: '#tb',
        columns: [[
            {field: "ck", checkbox: true},//显示多选框样式
            {field: "createDate", title: "创建时间", width: 100,sortable: true},
            {field: "followUp", title: "跟进效果", width: 100 ,formatter: function (value, row, index) {
                    if (value==0) {
                        return "很好";
                    } else if (value==1) {
                        return "一般";
                    }
                }
            },
            {
                field: "client", title: "客户", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "获取客户名字失败";
                    }
                }
            },
            {
                field: "input", title: "录入人", width: 100, formatter: function (value, row, index) {
                    if (value) {
                        return value.username;
                    } else {
                        return "获取录入人名字失败";
                    }
                }
            },
            {field: "implement", title: "实施方式", width: 100, formatter: function (value, row, index) {
                if (value) {
                    return value.name;
                } else {
                    return "电话沟通";
                }
            }
            },
            {field: "planTheme", title: "计划主题", width: 100},
            {field: "remark", title: "备注", width: 100}
        ]],

    });
    plandevelopment_dialog.dialog({
        width: 350,
        height: 400,
        buttons: "#bb",
        closed: true
    })

})
//工具栏的高级查询

function query() {

    /*客户名称*/
    var cbn = $("#clientByName").textbox("getValue");

    /*操作人*/
    var lun = $("#leadingUserName").textbox("getValue");


    /*根据创建时间查询*/
    /*开始时间*/
    var bd = $("#beginDate").textbox("getValue");
    /*结束时间*/
    var ed = $("#endDate").textbox("getValue");
    $("#plandevelopment_datagrid").datagrid("load", {
        clientByName: cbn,
        leadingUserName: lun,
        beginDate: bd,
        endDate: ed
    });
}

